package com.wangjw.tuling.netty.rpc.client;

import com.wangjw.tuling.netty.rpc.common.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.SynchronousQueue;

/**
 * Created by wangjianwen on 2018/12/11.
 */
public class NettyClientHandler extends SimpleChannelInboundHandler<RpcResponse> {

    private static final Logger logger = LoggerFactory.getLogger(NettyClientHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse rpcResponse) throws Exception {
        String id = rpcResponse.getId();
        SynchronousQueue<RpcResponse> synchronousQueue = NettyClient.getSynchronousQueue(id);
        synchronousQueue.put(rpcResponse);
        NettyClient.removeById(id);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.error("远程连接异常......");
        ctx.close();
    }
}
